package com.xuxin.baby.model;

import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.UploadFilesBean;
import com.xuxin.baby.contract.UploadFilesContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.UploadFilesPresenter;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MultipartBody;

/**
 * Created by xu_xin
 * Created on 2020/10/29 0029
 * 上传图片
 */
public class UploadFilesModel implements UploadFilesContract.Model {
    @Override
    public void uploadFiles(UploadFilesPresenter presenter, String tenantId, String token, String objectId, String objectName, String objectField, List<MultipartBody.Part> parts) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .uploadFiles(tenantId, token, "1",objectId, objectName, objectField, parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<UploadFilesBean>() {
                    @Override
                    public void onNextX(@NonNull UploadFilesBean uploadFilesBean) {
                        if (uploadFilesBean.isSuccess()) {
                            presenter.onSuccess(uploadFilesBean);
                        } else {
                            presenter.onError(uploadFilesBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}